import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createBinaryTree() {
        TreeNode A =   new TreeNode('A');
        TreeNode B =   new TreeNode('B');
        TreeNode C =   new TreeNode('C');
        TreeNode D =   new TreeNode('D');
        TreeNode E =   new TreeNode('E');
        TreeNode F =   new TreeNode('F');
        TreeNode G =   new TreeNode('G');
        TreeNode H =   new TreeNode('H');

        A.left = B;
        A.right = C;

        B.left = D;
        B.right = E;

        C.left = F;
        C.right = G;

        E.right = H;

        return A;
    }
    /**
     *根据二叉树创建字符串
     */
    public String tree2str(TreeNode root) {
        if(root == null) {
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder();
        tree2strChild(stringBuilder,root);
        return stringBuilder.toString();
    }
    public StringBuilder tree2strChild(StringBuilder stringBuilder,TreeNode root) {
        if(root == null) {
            return null;
        }
        stringBuilder.append(root.val);
        //左树
        if(root.left != null) {//不为空
            stringBuilder.append("(");
            tree2strChild(stringBuilder,root.left);
            stringBuilder.append(")");
        }else {//为空
            if(root.right != null) {
                stringBuilder.append("()");
            }else {
                return null;
            }
        }
        //右树
        if(root.right != null) {//不为空
            stringBuilder.append("(");
            tree2strChild(stringBuilder,root.right);
            stringBuilder.append(")");
        }else {//为空
            return null;
        }
        return stringBuilder;
    }
    /**
     二叉树的前序遍历
     1.判断是否为null
     2.判左
     3.判右
     */
    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if(root == null) {
            return list;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;

        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {//左不为null
                list.add(cur.val);
                stack.push(cur);
                cur = cur.left;
            }
            //为null,弹回到前一个节点位置,判断右的左右
            top = stack.pop();
            cur = top.right;
        }
        return list;
    }
    /**
     * 中序非递归
     */
    /**
     二叉树的中序遍历
     */
    public List<Character> inorderTraversal(TreeNode root) {

        List<Character> list = new ArrayList<>();
        if(root == null) {
            return list;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;

        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {//左不为null
                stack.push(cur);
                cur = cur.left;
            }
            //为null,弹回到前一个节点位置,判断右的左右
            top = stack.pop();
            list.add(top.val);
            cur = top.right;
        }
        return list;
    }

    /**
     * 后序遍历
     * @param root
     * @return
     */
    public List<Character> postorderTraversal(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if(root == null) {
            return list;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;

        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {//左不为null
                stack.push(cur);
                cur = cur.left;
            }
            //为null,弹回到前一个节点位置,判断右的左右
            TreeNode top = stack.peek();

            if(top.right == null || top.right == prev) {
                stack.pop();
                list.add(top.val);
                prev = top;
            }else {
                cur = top.right;
            }
        }
        return list;
    }
}
